package com.ai_daily.aitiantian.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.ai_daily.aitiantian.R;


/**
 * @version 1.0 升级提示框
 */


public class UpdateDialog extends Dialog implements View.OnClickListener {
	public Activity activity;
	private String mMainTitle;
    private String mSubTitle,mContent,mVersionTips;
    private String stringPositive, stringNegative;
	private View.OnClickListener negativeListener;
	private View.OnClickListener positiveListener;
    private Button btnOk;
    private Button btnCancel;

    private View vDividingLine;
    public UpdateDialog(Activity activity) {
        super(activity);
        this.activity = activity;
    }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.layout_update_dialog);
        Window window = getWindow();
        if(window != null){
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            initLayout();
        }
	}

	private void initLayout() {
		TextView mainTitleView = findViewById(R.id.tv_title);
        if(!TextUtils.isEmpty(mMainTitle)) {
            mainTitleView.setText(mMainTitle);
        }

		TextView subTitleView = findViewById(R.id.tv_subtitle);
		if(!TextUtils.isEmpty(mSubTitle)){
            subTitleView.setText(mSubTitle);
            subTitleView.setVisibility(View.VISIBLE);
		}

        TextView contentView = findViewById(R.id.tv_content);
		if(!TextUtils.isEmpty(mContent)) {
            Log.d("TEST",mContent);
//            mContent = "更新 \\n 1.xxxx \\n 2xxxxxxxx \\n 3xxxxxxx";
            Log.d("TEST",mContent);
            contentView.setText(mContent);
        }
        TextView versionTips = findViewById(R.id.version_tips);
        versionTips.setText(activity.getString(R.string.update_find_new, mVersionTips));

        btnOk = findViewById(R.id.btn_confirm);
        btnCancel = findViewById(R.id.btn_cancel);
        if(!TextUtils.isEmpty(stringPositive)) {
            btnOk.setText(stringPositive);
        }
        vDividingLine = findViewById(R.id.v_dividing_line);
        btnOk.setOnClickListener(this);
        if(!TextUtils.isEmpty(stringNegative)){
            btnCancel.setText(stringNegative);
        }
        btnCancel.setOnClickListener(this);

    }

    public UpdateDialog setMainTitle(String text) {
        this.mMainTitle = text;
        return this;
    }

    public UpdateDialog setSubTitle(String text) {
        this.mSubTitle = text;
        return this;
    }

    public UpdateDialog setContent(String text) {
        this.mContent = text;
        return this;
    }

    public UpdateDialog setVersionTips(String mVersionTips) {
        this.mVersionTips = mVersionTips;
        return this;
    }

    public UpdateDialog setPositiveListener(String textId, View.OnClickListener positiveListener) {
        this.stringPositive = textId;
        this.positiveListener = positiveListener;
        return this;
    }

    public UpdateDialog setNegativeListener(String textId, View.OnClickListener cancelListener) {
        this.stringNegative = textId;
        this.negativeListener = cancelListener;
        return this;
    }


    public Button getCancelButton() {
        return btnCancel;
    }

    public View getDividingLine() {
        return vDividingLine;
    }


    @Override
	public void onClick(View arg0) {

        switch (arg0.getId()) {
            case R.id.btn_confirm:
                if (positiveListener != null) {
                    positiveListener.onClick(arg0);
                }
                break;
            case R.id.btn_cancel:
                if (negativeListener != null) {
                    negativeListener.onClick(arg0);
                }
                dismiss();
                break;
            default:
                break;
        }

    }

}
